package com.haiyou.common.util;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 日期工具类
 *
 * @author root
 * @date 2019-03-09 19:01:19
 */
public class DateUtil {

    public static final String TIME_PATTERN = "HH:mm:ss";
    public static final String DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss";
    public static final String MINUTE_PATTERN = "yyyy-MM-dd HH:mm";
    public static final String HOUR_PATTERN = "yyyy-MM-dd HH";
    public static final String DATE_PATTERN = "yyyy-MM-dd";
    public static final String MONTH_PATTERN = "yyyy-MM";
    public static final String YEAR_PATTERN = "yyyy";
    public static final String YEAR_PATTERN2 = "yyyyMM";
    public static final String DATE_PATTERN2 = "yyyyMMdd";

    /**
     * 日期格式化
     *
     * @return 返回日期格式化后的字符串
     */
    public static String dateFormat() {
        return dateFormat(null, null);
    }

    public static Date strToDate(String date, String pattern) {
        SimpleDateFormat sDateFormat = new SimpleDateFormat(pattern);
        try {
            return sDateFormat.parse(date);
        } catch (ParseException e) {
            return null;
        }
    }

    /**
     * 日期格式化
     *
     * @param date    日期时间
     * @param pattern 日期格式
     * @return 返回日期格式化后的字符串
     */
    public static String dateFormat(Date date, String pattern) {
        if (date == null) {
            //默认当前时间
            date = new Date();
        }
        if (pattern == null) {
            // 默认日期格式化
            pattern = DATE_TIME_PATTERN;
        }
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        return sdf.format(date);
    }

    /**
     * 获取本周的开始时间
     *
     * @return
     */
    public static Date getBeginDayOfWeek() {
        Date date = new Date();
        if (date == null) {
            return null;
        }
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int dayofweek = cal.get(Calendar.DAY_OF_WEEK);
        if (dayofweek == 1) {
            dayofweek += 7;
        }
        cal.add(Calendar.DATE, 2 - dayofweek);
        return getDayStartTime(cal.getTime());
    }

    /**
     * 获取某个日期的开始时间
     *
     * @param d
     * @return
     */
    public static Timestamp getDayStartTime(Date d) {
        Calendar calendar = Calendar.getInstance();
        if (null != d) {
            calendar.setTime(d);
        }
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return new Timestamp(calendar.getTimeInMillis());
    }

    public static Date getCurrentDate() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }

    /**
     * 获取本周的结束时间
     *
     * @return
     */
    public static Date getEndDayOfWeek() {
        Calendar cal = Calendar.getInstance();
        cal.setTime(getBeginDayOfWeek());
        cal.add(Calendar.DAY_OF_WEEK, 6);
        Date weekEndSta = cal.getTime();
        return getDayEndTime(weekEndSta);
    }

    /**
     * 获取某个日期的结束时间
     *
     * @param d
     * @return
     */
    public static Timestamp getDayEndTime(Date d) {
        Calendar calendar = Calendar.getInstance();
        if (null != d) {
            calendar.setTime(d);
        }
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 23, 59, 59);
        calendar.set(Calendar.MILLISECOND, 999);
        return new Timestamp(calendar.getTimeInMillis());
    }


    /**
     * 用当天的日期的long型字符数串减去昨天日期long型字符数串
     * 其中：
     * 86400000L，它的意思是说1天的时间=24小时 x 60分钟 x 60秒 x 1000毫秒 单位是L。
     *
     * @return
     */
    public static String getYesterdayByDate(int day) {
        //实例化当天的日期
        Date today = new Date();
        //用当天的日期减去昨天的日期
        Date yesterdayDate = new Date(today.getTime() - day * 86400000L);
        String yesterday = new SimpleDateFormat(DATE_TIME_PATTERN).format(yesterdayDate);
        return yesterday;
    }

    /**
     * @param incr 增长因子，单位为天
     * @return
     */
    public static String getDate(int incr) {
        Date today = new Date();
        Date yesterdayDate = new Date(today.getTime() + incr * 86400000L);
        String yesterday = new SimpleDateFormat(DATE_PATTERN).format(yesterdayDate);
        return yesterday;
    }

    public static List<String> getBetweenDate(String begin, String end) {
        SimpleDateFormat format = new SimpleDateFormat(DATE_PATTERN);
        List<String> betweenList = new ArrayList<String>();

        try {
            Calendar startDay = Calendar.getInstance();
            startDay.setTime(format.parse(begin));
            startDay.add(Calendar.DATE, -1);

            while (true) {
                startDay.add(Calendar.DATE, 1);
                Date newDate = startDay.getTime();
                String newend = format.format(newDate);
                betweenList.add(newend);
                if (end.equals(newend)) {
                    break;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return betweenList;
    }


    public static void main(String[] args) {
//        String dateString = dateFormat(new Date(), DATE_PATTERN);
//        System.out.println(dateString);
//
//        List<String> list = getBetweenDate("2019-04-27", "2019-05-01");
//        System.out.println(list);
//        Map<String, Integer> map = new HashMap<>();
//        for (String date : list) {
//            map.put(date, 1);
//        }
//        System.out.println(map);

//        System.out.println(dateFormat(new Date(), DATE_PATTERN));
//        Date begin = DateUtil.getBeginDayOfWeek();
//        Date end = DateUtil.getEndDayOfWeek();
//        System.out.println("begin==>" + begin + ",end=" + end);
    }
}